import requestChangeErrorLogReadFlagApi from '@/api/request/requestChangeErrorLogReadFlagApi'
import { CEllipsisText } from '@/components/CTable/CTable'
import IEsErrorVO from '@/model/vo/request/IEsErrorVO'
import {
  getNameByValueAndDictList,
  handlerRegion,
  ToastSuccess,
} from '@/util/CommonUtil'
import { IconRadio } from '@douyinfe/semi-icons'
import { Modal, Typography } from '@douyinfe/semi-ui'
import { OptionProps } from '@douyinfe/semi-ui/lib/es/select'

export default (dictList: OptionProps[], doGetTableData: () => void) => [
  { title: 'id', dataIndex: '_id', width: '120px' },
  { title: 'uri', dataIndex: 'uri', width: '130px' },
  { title: '请求人id', dataIndex: 'createId', width: '130px' },
  { title: 'ip', dataIndex: 'ip', width: '100px' },
  {
    title: 'ip区域',
    width: '150px',
    dataIndex: 'region',
    render: (text: any, record: IEsErrorVO, index: number) => {
      return (
        <CEllipsisText text={handlerRegion(record.region!)}></CEllipsisText>
      )
    },
  },
  { title: '请求的服务名', dataIndex: 'service', width: '130px' },
  { title: '主机的地址', dataIndex: 'address', width: '130px' },
  { title: '端口', dataIndex: 'port', width: '70px' },
  {
    title: '错误信息',
    dataIndex: 'errorMsg',
    render: (text: any, record: IEsErrorVO, index: number) => {
      const errorMsgTemp = record.errorMsg?.split(' ')[0] // 截取一部分的错误信息
      return (
        <Typography.Text
          title="点击查看详情"
          ellipsis={{
            showTooltip: {
              opts: {
                content: errorMsgTemp,
                className: 'wb-w w100',
              },
            },
          }}
          link
          onClick={() => {
            Modal.error({
              title: '错误信息',
              content: record.errorMsg,
              hasCancel: false,
              fullScreen: true,
              closeOnEsc: true,
            })
          }}
        >
          {errorMsgTemp}
        </Typography.Text>
      )
    },
  },
  {
    title: '类别',
    width: '110px',
    dataIndex: 'category',
    render: (text: any, record: IEsErrorVO, index: number) => {
      return getNameByValueAndDictList(dictList, record.category!)
    },
  },
  {
    title: '已读',
    dataIndex: 'readFlag',
    width: '70px',
    render: (text: any, record: IEsErrorVO, index: number) => {
      return (
        <IconRadio
          title={(record.readFlag ? '错误已读' : '错误未读') + '，点击切换状态'}
          className={'hand ' + (record.readFlag ? 'green' : 'red')}
          onClick={() => {
            requestChangeErrorLogReadFlagApi({
              id: record._id,
              flag: !record.readFlag,
            }).then(({ data }) => {
              ToastSuccess(data.msg)
              doGetTableData()
            })
          }}
        />
      )
    },
  },
  { title: '创建时间', dataIndex: 'createTime', width: '170px' },
]
